//
//  CommonLabelCollectionReusableView.m
//  MiaoTu
//
//  Created by dllo on 16/8/31.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CommonLabelCollectionReusableView.h"
#import "Masonry.h"
#import "CommonCollectionCell.h"

#define kCommonCollectionCell @"CommonCollectionCellIdentifier"

@interface CommonLabelCollectionReusableView () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UITextFieldDelegate>
@property (nonatomic, strong) UITextField *yourLabelTextField;
@property (nonatomic, strong) UIButton *addButton;
@property (nonatomic, strong) UILabel *commomLabel;
@property (nonatomic, strong) UICollectionView *commonCollection;
@property (nonatomic, strong) NSArray *labelArray;
@end

@implementation CommonLabelCollectionReusableView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        _yourLabelTextField = [[UITextField alloc] init];
        [self addSubview:_yourLabelTextField];
        _addButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [self addSubview:_addButton];
        _commomLabel = [[UILabel alloc] init];
        [self addSubview:_commomLabel];
        UICollectionViewFlowLayout *fl = [[UICollectionViewFlowLayout alloc] init];
        fl.itemSize = CGSizeMake(43, 34);
        _commonCollection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:fl];
        [self addSubview:_commonCollection];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.backgroundColor = [UIColor whiteColor];
    [_addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.mas_right).offset(-60);
        make.top.mas_equalTo(self.mas_top);
        make.height.mas_equalTo(33);
        make.width.mas_equalTo(60);
    }];
    _addButton.layer.cornerRadius = 8;
    _addButton.layer.masksToBounds = YES;
    _addButton.backgroundColor = [UIColor colorWithRed:0.937 green:0.643 blue:0.282 alpha:1.000];
    [_addButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_addButton setTitle:@"添加" forState:UIControlStateNormal];
    [_addButton addTarget:self action:@selector(addLableAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [_yourLabelTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(_addButton.mas_left).offset(-15);
        make.height.mas_equalTo(_addButton);
    }];
    _yourLabelTextField.backgroundColor = [UIColor colorWithWhite:0.929 alpha:1.000];
    _yourLabelTextField.placeholder = @"请添加您的标签(选填)";
    _yourLabelTextField.layer.cornerRadius = 2;
    _yourLabelTextField.layer.masksToBounds = YES;
    _yourLabelTextField.delegate = self;
    
    [_commomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(_yourLabelTextField);
        make.top.mas_equalTo(_yourLabelTextField.mas_bottom).offset(20);
    }];
    _commomLabel.textColor = [UIColor colorWithWhite:0.827 alpha:1.000];
    _commomLabel.text = @"常用标签";
    
    [_commonCollection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_commomLabel);
        make.right.mas_equalTo(_addButton);
        make.top.mas_equalTo(_commomLabel.mas_bottom).offset(20);
        make.bottom.mas_equalTo(self.mas_bottom);
    }];
    _commonCollection.dataSource = self;
    _commonCollection.delegate = self;
    self.labelArray = @[@"驴友", @"摄影师", @"小资", @"旅游达人", @"暖男", @"小鲜肉", @"小公举", @"单身狗", @"蛇精病", @"小清新", @"铲屎官"];
    
    [_commonCollection registerNib:[UINib nibWithNibName:@"CommonCollectionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kCommonCollectionCell];
    _commonCollection.backgroundColor = [UIColor whiteColor];
}

- (void)addLableAction:(UIButton *)button {
    [self.delegate sendLabelValue:_yourLabelTextField.text];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _labelArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CommonCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCommonCollectionCell forIndexPath:indexPath];
    cell.titleLabel.text = _labelArray[indexPath.row];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGRect newRect = [_labelArray[indexPath.row] boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 50) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17]} context:nil];
    newRect.size.height += 10;
    newRect.size.width += 15;
    return newRect.size;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%@", _labelArray[indexPath.row]);
    [self.delegate sendLabelValue:_labelArray[indexPath.row]];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField endEditing:YES];
    return YES;
}

@end
